//
//  searchCameraViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "searchCameraViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "MyCloudView.h"
#import "searchresultViewController.h"
@interface searchCameraViewController ()

@property(nonatomic,retain)NSMutableArray *nsarr;

@property(nonatomic,retain)MyCloudView *cloudview;

@property(nonatomic,retain)UITextField *field;

@property(nonatomic,retain)NSMutableArray *Arr;

@end

@implementation searchCameraViewController

-(void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=hotword" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.nsarr = responseObject;
        self.cloudview = [[MyCloudView alloc] initWithFrame:CGRectMake(0, 110, self.view.frame.size.width, 473)];
        [self.view addSubview:self.cloudview];
        [_cloudview release];
        self.cloudview.backgroundColor = [UIColor whiteColor];
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeview:)];
        [self.cloudview addGestureRecognizer:swipe];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeview:)];
        [self.cloudview addGestureRecognizer:swipe2];
        swipe2.direction = UISwipeGestureRecognizerDirectionDown;
        UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeview:)];
        [self.cloudview addGestureRecognizer:swipe3];
        swipe3.direction = UISwipeGestureRecognizerDirectionUp;
        UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeview:)];
        [self.cloudview addGestureRecognizer:swipe4];
        swipe4.direction = UISwipeGestureRecognizerDirectionLeft;
        [swipe release];
        [swipe2 release];
        [swipe3 release];
        [swipe4 release];
        self.cloudview.titles = self.nsarr;
         [self.view addSubview:self.myTableView];
        [self.cloudview generate];
        [self.cloudview setTagClickBlock:^(NSString *title, NSInteger tag) {
            self.field.text = title;
            }];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"加载失败");
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"搜索";
    [self data];
    [self setview];
    [self setview2];
}

-(void)setview{
    
    
    self.field = [[UITextField alloc] initWithFrame:CGRectMake(15, 77, 285, 30)];
    [self.view addSubview:self.field];
    [_field release];
    self.field.placeholder = @"请输入... ...";
    self.field.layer.borderWidth = 1;
    self.field.layer.cornerRadius = 5;
    self.field.clearButtonMode = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:self.field];
    
    UIButton *bu = [UIButton buttonWithType:UIButtonTypeSystem];
    bu.frame = CGRectMake(310, 78, 55, 28);
    [bu setTitle:@"搜索" forState:UIControlStateNormal];
    [self.view addSubview:bu];
    bu.layer.borderWidth = 1;
    bu.layer.cornerRadius = 3;
    bu.titleLabel.textColor = [UIColor blackColor];
    [bu addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *bu2 = [UIButton buttonWithType:UIButtonTypeSystem];
    bu2.frame = CGRectMake(5, 583, 365, 35);
    bu2.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:bu2];
    [bu2 setTitle:@"换一换" forState:UIControlStateNormal];
    bu2.titleLabel.textAlignment = 1;
    bu2.titleLabel.textColor = [UIColor whiteColor];
    self.field.delegate = self;
    bu2.tag = 1002;
    bu.tag = 1001;
    [bu2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)setview2{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(15, 107, 285, 120) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.hidden = YES;
    [self.myTableView release];
    self.myTableView.backgroundColor  = [UIColor whiteColor];
    self.myTableView.layer.borderWidth = 1;
}

-(void)textFieldChanged:(id)santer{
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    self.myTableView.hidden = YES;
           NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Cartoon&ios=23&pad=0&a=search&word=%@&page=1&limit=20&type=0",self.field.text];
       NSString *str1 =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  [manager GET:str1 parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
           NSMutableDictionary *dic = responseObject;
            self.Arr = [NSMutableArray array];
            NSMutableArray *arr1 = [dic objectForKey:@"list"];
            for (NSMutableDictionary *dic in arr1) {
                NSString *str = [dic objectForKey:@"name"];
                NSString *str1 = [str substringToIndex:1];
               if ([str1 isEqualToString:self.field.text]) {
                    [self.Arr addObject:dic];
                }
            }
        if (self.Arr.count > 0) {
            
            self.myTableView.hidden = NO;
            self.myTableView.frame = CGRectMake(15, 107, 285, self.Arr.count*20);
        }
            [self.myTableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"解析失败");
        }];
}


-(void)swipeview:(UISwipeGestureRecognizer *)swipe{
    [self.cloudview generate];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}


-(void)click:(UIButton *)santer{
    if (santer.tag == 1002) {
        [self.cloudview generate];
    }else{
        searchresultViewController *result = [[searchresultViewController alloc] init];
        result.canshu = self.field.text;
        [self.navigationController pushViewController:result animated:YES];
        [result release];
  }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = [self.Arr[indexPath.row] objectForKey:@"name"];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.Arr[indexPath.row] objectForKey:@"name"];
    self.field.text = str;
    searchresultViewController *result = [[searchresultViewController alloc] init];
    result.canshu = self.field.text;
    [self.navigationController pushViewController:result animated:YES];
    [result release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
